<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('配置')" :v-if="dialogVisible" :visible.sync="dialogVisible" class="arpDefenseDialog" width="555px" top="20vh" @close='close'>
    <div>
      <el-form ref="form" :model="record" label-width="175px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('主动防御状态')" prop="state">
          <el-switch :width="45" v-model="record.state" class="state switch" active-color="#009966" inactive-color="#CCCCCC" active-value="enable" inactive-value="disable" :active-text="$t('开')" :inactive-text="$t('关')"></el-switch>
        </el-form-item>
        <el-form-item :label="$t('通告接口')" prop="defense_interface">
          <el-select v-model="record.defense_interface" :disabled='record.state === "disable"' class="arpLength" :placeholder="$t('请选择出接口')" clearable size='small'>
            <el-option v-for="item in defenseInterfaceList" :disabled="item.enPortMode === 3" :key="item.strPhysicsName" :label="item.strPhysicsName" :value="item.strPhysicsName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('通告地址')" prop="notify_address">
          <el-input v-emoji v-model="record.notify_address" :disabled='record.state === "disable"' class="arpLength" size='small' ref="notifyAddress"></el-input>
        </el-form-item>
        <el-form-item :label="$t('通告间隔时间')" prop="notify_interval">
          <el-input v-emoji v-model="record.notify_interval" :disabled='record.state === "disable"' class="arpLength" size='small' ref="notifyInterval">
            <template slot="append">{{$t('秒')}}</template>
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getArpDefenseInfo, setArpDefenseInfo } from '@/api/safe/bind'
import { getPhysicalApiArray } from '@/api/network/api/physical'
import { inputClass } from '@/utils/input-class.js'
export default {
  name: 'BindDialog',
  data () {
    var validateNotifyInterval = (rule, value, callback) => {
      if (this.record.state === 'enable') {
        if (!String(value).trim()) {
          callback(new Error(this.$t('不能为空')))
        } else if (isNaN(Number(value))) {
          callback(new Error(this.$t('请输入数字')))
        } else if (Number(value) < 0.1 || Number(value) > 60) {
          callback(new Error(this.$t('通告时间间隔范围必须在0.1-60之间')))
        } else if (String(value).indexOf('.') === -1 ? false : ((String(value).length - String(value).indexOf('.')) > 3)) {
          callback(new Error(this.$t('只能输入小数点后两位')))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        id: '',
        state: 'disable',
        defense_interface: '',
        notify_address: '',
        notify_interval: ''
      },
      recordCopy: {
        id: '',
        state: 'disable',
        defense_interface: '',
        notify_address: '',
        notify_interval: ''
      },
      defenseInterfaceList: [],
      rules: {
        notify_interval: [{ required: false, validator: validateNotifyInterval, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['notifyAddress', 'notifyInterval']
        inputClass(this.$refs, data)
      })
      getPhysicalApiArray().then(res => {
        if (res.status === 0) {
          this.defenseInterfaceList = res.info
        }
      })
      if (data) {
        this.record.id = data.id
        getArpDefenseInfo({ id: data.id }).then(res => {
          if (res.status === 0) {
            this.record.state = res.info[0].state
            this.record.defense_interface = res.info[0]['defense-interface']
            this.record.notify_address = res.info[0]['notify-address']
            this.record.notify_interval = res.info[0]['notify-interval']
          }
        })
      }
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          setArpDefenseInfo(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('配置成功'),
                type: 'success'
              })
              this.close()
              this.$parent.listData()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.defenseInterfaceList = []
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style>
.arpLength {
  width: 270px;
}
</style>
